﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
using System.Text;

using System.Globalization;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Sys_InfoController : Controller
    {

        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }
        public string List()
        {
            SystemInfo info = new SystemInfo();

            #region 服务器探针
            info.ServerName = "http://" + this.ControllerContext.RequestContext.HttpContext.Request.Url.Host + this.ControllerContext.RequestContext.HttpContext.Request.ApplicationPath;
            info.Ip = Request.ServerVariables["LOCAl_ADDR"];
            info.Domain = Request.ServerVariables["SERVER_NAME"].ToString();
            info.Port = Request.ServerVariables["Server_Port"].ToString();
            info.IISVer = Request.ServerVariables["Server_SoftWare"].ToString();
            info.PhPath = Request.PhysicalApplicationPath;
            info.Operat = Environment.OSVersion.ToString();
            info.SystemPath = Environment.SystemDirectory.ToString();
            info.TimeOut = (Server.ScriptTimeout / 1000).ToString() + "秒";
            info.Lan = CultureInfo.InstalledUICulture.EnglishName;
            info.AspnetVer = string.Concat(new object[] { Environment.Version.Major, ".", Environment.Version.Minor, Environment.Version.Build, ".", Environment.Version.Revision });
            info.CurrentTime = DateTime.Now.ToString();

            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Version Vector");
            info.IEVer = key.GetValue("IE", "未检测到").ToString();
            info.ServerLastStartToNow = ((Environment.TickCount / 0x3e8) / 60/60).ToString() + "小时";
            
            string[] achDrives = Directory.GetLogicalDrives();
            for (int i = 0; i < Directory.GetLogicalDrives().Length - 1; i++)
            {
                info.LogicDriver = info.LogicDriver + achDrives[i].ToString();
            }

            info.CpuNum = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS").ToString();
            info.CpuType = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").ToString();
            info.AppRunTime = GetAppRunTime;
            info.CacheMax = CMN.Cache.CMNCache.Instance().Remains;
            info.Memory = (Environment.WorkingSet / 1024 / 1024).ToString() + "M";
            info.MemoryPro = ((Double)GC.GetTotalMemory(false) / 1048576).ToString("N2") + "M";
            System.Diagnostics.Process ps = System.Diagnostics.Process.GetCurrentProcess();
            info.AppRunMemony = ((Double)ps.WorkingSet64 / 1048576).ToString("N2") + "M";
            info.AppRunVirtualMemony = string.Format("{0}M", ps.VirtualMemorySize64 / 1024 / 1024);
            ps.Dispose();
            info.SessionNum = Session.Contents.Count.ToString();
            info.Session = Session.Contents.SessionID;
            info.User = Environment.UserName;
            #endregion

            return string.Concat("{success:true,data:", JsonConvert.SerializeObject(info), "}");
        }

        private string GetAppRunTime
        {
            get
            {
                TimeSpan span = DateTime.Now - CMN.Controllers.View.MvcViewEngine.AppStartTime;
                string result = span.Days.ToString() + "天 ";
                result += span.Hours.ToString() + "小时 ";
                result += span.Minutes.ToString() + "分 ";
                result += span.Seconds.ToString() + "秒";
                return result;
            }
        }

    }
}
